﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Collections.Specialized;

namespace Agro.Library {
    public static class WebUtility {

        public const string DESKEY = "ACEGIKMO";

        public static Dictionary<string, string> ConvertQueryToDictionary(string query)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            if (string.IsNullOrWhiteSpace(query)) return dic;
            query = HttpContext.Current.Server.UrlDecode(query);
            string[] text = query.Split('&');
            foreach (var item in text)
            {
                string[] values = item.Split('=');
                if (string.IsNullOrEmpty(values[1]))
                    continue;
                if (dic.ContainsKey(values[0]))
                {
                    dic[values[0]] = string.Format("{0},{1}", dic[values[0]], values[1]);
                }
                else
                    dic.Add(values[0], values[1]);
            }
            return dic;
        }

        public static FormCollection ConvertToFormCollection(string query) {
            if (string.IsNullOrEmpty(query)) {
                return new FormCollection();
            }
            NameValueCollection v = GetValueCollection(query);
            return new FormCollection(v);
        }

        public static NameValueCollection GetValueCollection(string query) {
            NameValueCollection v = new NameValueCollection();
            if (string.IsNullOrEmpty(query)) {
                return v;
            }
            foreach (var s in query.Split('&')) {
                var ss = s.Split('=');
                if (ss.Length > 1) {
                    v.Add(ss[0], System.Web.HttpContext.Current.Server.UrlDecode(ss[1]));
                }
            }
            return v;
        }

        public static string UrlParasToWhereString() {
            return UrlParasToWhereString(null, null);
        }

        public static string UrlParasToWhereString(string prefix) {
            return UrlParasToWhereString(prefix, null);
        }

        public static string UrlParasToWhereString(string prefix, string[] removeKeys) {
            string whereStr = " 1=1 ";
            var collection = HttpContext.Current.Request.QueryString;
            if (collection.AllKeys.Length == 0) return string.Empty;
            foreach (var item in collection.AllKeys) {
                if (string.IsNullOrEmpty(item)) continue;
                string val = collection[item];
                if (removeKeys != null && removeKeys.Contains(item)) continue;
                if (!string.IsNullOrEmpty(prefix)) {
                    if (item.ToUpper().Contains(prefix.ToUpper())) {
                        if (val.Contains(";")) {
                            whereStr += " AND " + item + " IN (" +
                                string.Join(",", (val.Split(';').Select(m => string.Format("'{0}'", SN.Core.Helper.StringHelper.SqlReplace(m))).ToArray())) + ")";
                        }
                        else {
                            whereStr += " AND " + item + "=" + string.Format("'{0}'", SN.Core.Helper.StringHelper.SqlReplace(val));
                        }
                    }
                }
                else {
                    if (val.Contains(";")) {
                        whereStr += " AND " + item + " IN (" +
                            string.Join(",", (val.Split(';').Select(m => string.Format("'{0}'", SN.Core.Helper.StringHelper.SqlReplace(m))).ToArray())) + ")";
                    }
                    else {
                        whereStr += " AND " + item + "=" + string.Format("'{0}'", SN.Core.Helper.StringHelper.SqlReplace(val));
                    }
                }
            }
            return whereStr;
        }

    }
}
